from django.contrib import admin
from gxxs.models import *
from import_export.admin import ImportExportModelAdmin, ExportActionModelAdmin
from gxxs.resources import *
from reversion.admin import VersionAdmin
import logging
from datetime import datetime
from django.contrib import messages
# Register your models here.


class Gxxs0701Inline(admin.StackedInline):
    model = Gxxs0701
    extra = 0
    list_display = ['xh', 'rxny', 'xslbm', 'yxsh', 'szbh', 'zym', 'xsdqztm', 'update_time']
    search_fields = ['xh']
    list_filter = ['rxny', 'xslbm', 'yxsh', 'szbh', 'zym', 'xsdqztm', 'lock', 'source']
    ordering = ('-update_time', )
    raw_id_fields = ('szbh', 'zym', )
    readonly_fields = ['create_time', 'update_time']

    fieldsets = (
        ('常用信息', {"fields": ["xh", "xslbm", ('sznj', 'rxny', 'xz'), ("yxsh", "zym", "szbh"), "xsdqztm"]}),
        ("学籍信息", {"fields": ['deyxmc', ('xkmlm', 'yjfx'), ('pyfsm', 'pyccm'), 'dsh', 'hdxlfsm', 'sfxfz', 'ldfsm']}),
        ("其他信息", {
            "classes": ('collapse',),
            "fields": ['source', 'priority', 'lock', 'remarks', 'create_time', 'update_time']
        }),
    )


@admin.register(Gxxs0101)
class Gxxs0101Admin(VersionAdmin, ImportExportModelAdmin, ExportActionModelAdmin):
    list_display = ['xh', 'xm', 'xbm', 'sznj', 'csrq', 'csdm', 'jgm', 'mzm', 'sfzjh', 'xslbm', 'xsdqztm', 'zp', 'lock', 'update_time']
    search_fields = ['xh', 'xm', 'gxxs0701__szbh__bjmc', 'gxxs0701__zym__zymc']
    list_filter = ['xbm', 'gxxs0701__sznj', 'gxxs0701__yxsh', 'gxxs0701__zym', 'gxxs0701__szbh', 'jgm', 'mzm', 'sfzjlxm', 'zzmmm', 'gxxs0701__xslbm', 'gxxs0701__xsdqztm','zp', 'lock', 'source']
    ordering = ('-update_time', )

    readonly_fields = ['create_time', 'update_time']
    inlines = [Gxxs0701Inline]
    resource_class = Gxxs0101Resource

    change_list_template = "admin/change_list_recover.html"

    fieldsets = (
        ('常用信息', {"fields": [
            "xh",
            "xm",
            "xbm",
            ("csrq", "csdm"),
            ("sfzjlxm", "sfzjh", "sfzjyxq"),
            ("jgm", "mzm"), "zp"
        ]}),
        ("基本信息", {"fields": [
            ("cym", "ywxm", "xmpy"),
            "gjdqm",
            "yhkh",
            "hyzkm",
            "gatqwm",
            "zzmmm",
            "jkzkm",
            "xyzjm",
            "xxm",
            "sfdszn"
        ]}),
        ("其他信息", {
            "classes": ('collapse',),
            "fields": ['source', 'priority', 'lock', 'remarks', 'create_time', 'update_time']
        }),
    )

    def sznj(self, obj):
        if not obj.gxxs0701:
            return None
        return obj.gxxs0701.sznj
    sznj.short_description = '年级'

    def xslbm(self, obj):
        if not obj.gxxs0701:
            return None
        return obj.gxxs0701.xslbm
    xslbm.short_description = '类别'

    def xsdqztm(self, obj):
        if not obj.gxxs0701:
            return None
        return obj.gxxs0701.xsdqztm

    xsdqztm.short_description = '状态'

    def save_model(self, request, obj, form, change):
        obj.lock = True

        if obj.sfzjlxm is not None and obj.sfzjlxm.value == '1' and obj.sfzjh and len(obj.sfzjh) == 18:
            if obj.csdm is None:
                obj.csdm = obj.sfzjh[0:6]
            if obj.csrq is None:
                csrq = '{}-{}-{}'.format(obj.sfzjh[6:10], obj.sfzjh[10:12], obj.sfzjh[12:14])
                obj.csrq = datetime.strptime(csrq, '%Y-%m-%d')

        super(Gxxs0101Admin, self).save_model(request, obj, form, change)


@admin.register(Gxxs0701)
class Gxxs0701Admin(VersionAdmin):
    list_display = ['xh', 'rxny', 'xslbm', 'yxsh', 'szbh', 'zym', 'xsdqztm', 'lock', 'update_time']
    search_fields = ['xh__xh', 'szbh__bjmc', 'zym__zymc']
    list_filter = ['rxny', 'xslbm', 'yxsh', 'szbh', 'zym', 'xsdqztm', 'lock', 'source']
    ordering = ('-update_time', )
    raw_id_fields = ('szbh', 'zym', )
    readonly_fields = ['xh', 'create_time', 'update_time']

    fieldsets = (
        ('常用信息', {"fields": ["xh", "xslbm", ('sznj', 'rxny', 'xz'), ("yxsh", "zym", "szbh"), "xsdqztm"]}),
        ("学籍信息", {"fields": ['deyxmc', ('xkmlm', 'yjfx'), ('pyfsm', 'pyccm'), 'dsh', 'hdxlfsm', 'sfxfz', 'ldfsm']}),
        ("其他信息", {
            "classes": ('collapse',),
            "fields": ['source', 'priority', 'lock', 'remarks', 'create_time', 'update_time']
        }),
    )

    def save_model(self, request, obj, form, change):
        obj.lock = True
        super(Gxxs0701Admin, self).save_model(request, obj, form, change)
